var rurl = "/upDataMain/";
var rwurl = "/upDataMainWeb/";

var fpid=0;//父窗口的pid
var flow = layui.flow;
var element = layui.element;
var upload = layui.upload;
var sortType;//
var sortStr;//ACE desc
var condition;
var rangeType;// more less mole
var rangeStr;//$$
var rangeField;
var myuid;//当前的选择主数据id
var fid=0;
var uploadurl="/upload/";

//图片列表
function showDetailImg(id) {
    //   var da = "{\"page\":1,\"limit\":10,\"name\":\"" + condition + "\"}";da = "{\"name\":\"" + condition + "\"}";
    var urlx = "/tFile/search/1/1000";
    var datax = "{\"uid\":\"" + id + "\",\"type\":\"image\"}";
    condition = id;
    $.ajax({
        url: urlx,
        type: "POST",
        data: datax,
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            console.log(data);
            table.reload('imglist', {
                data: data.data.list,
                limit: 1000
            });
            showimg(data.data.list);
        },
        error: function (data) {
            console.log('error');
        }
    });
}

//监听头工具栏事件 图片的
table.on('tool(imglist)', function (obj) {
    var data = obj.data;
    var type=data.type;
    var fname=data.fname;
    var dir =data.dir;
    if (obj.event === 'show') {//查看
        console.log(JSON.stringify(data));
        openimg(data.type,data.fname,data.dir);
    } else if (obj.event === 'download') {//下载
        var url = "/upload/"+type+"/"+dir+"/"+fname;
        downloadFile(url,fname);
    } else if (obj.event === 'upload') {//上传
        fid=data.id;
        uploadInit();
    }else if (obj.event === 'video') {//播放
        var url=data.videourl;
        openVideo(url);
    }
});

//图片显示
function showimg(imglist){
    console.log(imglist);//imgShowWidth imgShowHeight imgListShowheight(imgListShow)
    var imgShowWidth=$("#imgShowWidth").val();
    var imgShowHeight=$("#imgShowHeight").val();
    var imgListShowheight=$("#imgListShowHeight").val();
    $("#imgListShow").attr("style","overflow:auto;height:"+imgListShowheight+"px");
    $("#imgListShow").html("");
    for(var img of imglist){
        var imgurl=""+uploadurl+img.type+"/"+img.dir+"/"+img.fname;
        var imghtml= "<li><span>"+img.id+"</span> <img id="+img.id+" style=width:"+imgShowWidth+"px;height:"+imgShowHeight+"px lay-src=\""+imgurl+"\"></li>";
        $("#imgListShow").append(imghtml);
    }
    $(".flow-default li").attr("style","height:"+imgShowHeight+"px;line-height:"+imgShowHeight+"px");
    //按屏加载图片
    flow.lazyimg({
        elem: '#imgListShow img'
        ,scrollElem: '#imgListShow' //一般不用设置，此处只是演示需要。
        ,isAuto: false
        ,isLazyimg: true
    });
    fangdaimg();
}

//查询展示文件列表
function showDetailFile(id) {
    //   var da = "{\"page\":1,\"limit\":10,\"name\":\"" + condition + "\"}";da = "{\"name\":\"" + condition + "\"}";
    var urlx = "/tFile/search/1/1000";
    var datax = "{\"uid\":\"" + id + "\",\"type\":\"文件\"}";
    var fsname=$("#fsname").val();
    var fsdir=$("#fsdir").val();
    var fstype=$("#fstype").val();
    datax = "{\"uid\":\"" + id + "\",\"name\":\"" + fsname + "\",\"dir\":\"" + fsdir + "\",\"type\":\"" + fstype + "\"}";
    condition = id;
    $.ajax({
        url: urlx,
        type: "POST",
        data: datax,
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            table.reload('filelist', {
                data: data.data.list,
                toolbar:'#toolbarFile',
                limit: 1000
            });
        },
        error: function (data) {
            console.log('error');
        }
    });
}

//监听头工具栏事件 文件
table.on('toolbar(filelist)',
    function (obj) {
        var checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'uploadzip'://压缩打包下载
                if (data.length === 0) {
                    layer.msg('请选择一行');
                    return;
                }
                var fids="["+getFidsStr(checkStatus.data)+"]";
                var rurlx="/fileDownLoadZipController/downLoadZipFiles";
                $.ajax({
                    url: rurlx,
                    type: "POST",
                    data: fids,
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        console.log(data);
                        downloadFile("/"+data.downloadRUL,data.downloadRUL);
                    }
                });
                break;
            case 'update':
                toAddAction(checkStatus.data,"tfile");
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    console.log(checkStatus.data);
                    layer.confirm('真的删除行么', function (index) {
                        for (var deletedata of checkStatus.data) {
                            console.log(deletedata);
                            delDataFile(deletedata.id);
                        }
                        layer.close(index);
                     showDetail(condition);
                    });
                }
                break;
        }
    });

//监听单元格编辑,快速修改 文件
table.on('edit(filelist)', function (obj) {
    var value = obj.value //得到修改后的值
        , data = obj.data //得到所在行所有键值
        , field = obj.field; //得到字段
    layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
    quickUpdateDataRurl(data.id, field, value,"/tFile/");
});

//监听-条-工具栏事件 文件的
table.on('tool(filelist)', function (obj) {
    var data = obj.data;
    var type=data.type;
    var fname=data.fname;
    var dir =data.dir;
    if (obj.event === 'show') {//查看
        console.log(JSON.stringify(data));
        openimg(data.type,data.fname,data.dir);
    } else if (obj.event === 'download') {//下载
        var url = "/upload/"+type+"/"+dir+"/"+fname;
        downloadFile(url,fname);
    } else if (obj.event === 'upload') {//上传
        fid=data.id;
        uploadInit();
    }else if (obj.event === 'video') {//播放
       var url=data.videourl;
        openVideo(url);
    }
});

//监听-条-工具栏事件 子数据
table.on('tool(tmdlist)', function (obj) {
    var data = obj.data;
    var type=data.type;
    var fname=data.fname;
    var dir =data.dir;
    if (obj.event === 'opendown') {//
        var data = obj.data;
        opendown(data.id);
        console.log(data.id);
        //openimg(data.type,data.fname,data.dir);
    } else if (obj.event === 'download') {//下载
    } else if (obj.event === 'upload') {//上传
    }
});



//子数据窗口打开下级
function opendown(id){
    this.fpid=id;
    layer.open({
        type: 2
        , title: '详细页'
        , content: rwurl + "list"//页面路径
        , maxmin: true
        , area: [(document.body.clientWidth-100)+"px",( document.body.clientHeight-300)+"px"]
        , btn: ['确定', '取消']
        , yes: function (index, layero) {
            //点击确认触发 iframe 内容中的按钮提交
        }
    });
}


/*//监听头工具栏事件 文件的
table.on('tool(filelist)', function (obj) {
    var data = obj.data;
    var type=data.type;
    var fname=data.fname;
    var dir =data.dir;
    if (obj.event === 'show') {//查看
        console.log(JSON.stringify(data));
        openimg(data.type,data.fname,data.dir);
    } else if (obj.event === 'download') {//下载
        var url = "/upload/"+type+"/"+dir+"/"+fname;
        downloadFile(url,fname);
    } else if (obj.event === 'upload') {//上传
        fid=data.id;
        uploadInit();
    }
});*/

function quickUpdateDataRurl(id, field, value,rurl) {
    var data = "{\"id\":" + id + ",\"" + field + "\"" + ":\"" + value + "\"}";
    sendRequst("PUT", data, rurl + id);
}


//单个上传
function uploadInit() {
    upload.render({
        elem: '#uploadFile'
        , url: '/tFile/fileUpload' //改成您自己的上传接口
        , field: 'myfiles'
        ,accept:"file"
        , data: {uid: myuid,
            fid:fid
        }
        , done: function (res) {
            layer.msg('上传成功');
            showDetailImg(myuid);
            showDetailFile(myuid);
            console.log(res)
        }
    });
    $("#uploadFile").click();
}

//文件列表条件查询
form.on('submit(submitFileBtn)', function (data) {
    var condiction = JSON.stringify(data.field);
    showDetailFile(myuid);
    return false;
});

//查看所有详细子节点图片文件
function showDetail(id) {
    setTimeout("showDetailLater("+id+")",500);
}

function showDetailLater(id){
    var urlx = "/upDataMain/search/1/1000";
    var datax = "{\"upDataMain\":{\"parent_id\":\"" + id + "\"}}";
    condition = id;
    $.ajax({
        url: urlx,
        type: "POST",
        data: datax,
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            console.log(data);
            table.reload('tmdlist', {
                data: data.data.list,
                limit: 1000
            });
        },
        error: function (data) {
            console.log('error');
        }
    });
    showDetailImg(id);
    showDetailFile(id);
}

//删除文件
function delDataFile(id) {
    sendRequst("DELETE", "", "/tFile/" + id);
}



//监听行单击事件 子数据（双击事件为：rowDouble，单击事件：row）
table.on('rowDouble(tmdlist)', function (obj) {
    var data = obj.data;
    //标注选中样式
    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
    toEdit = data;
    isadd = false;
    openEdit();
});

//监听单元格编辑,快速修改 子数据
table.on('edit(tmdlist)', function (obj) {
    var value = obj.value //得到修改后的值
        , data = obj.data //得到所在行所有键值
        , field = obj.field; //得到字段
    layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改为：' + value);
    quickUpdateData(data.id, field, value);
});


//监听头工具栏事件 子数据
table.on('toolbar(tmdlist)', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id)
        , data = checkStatus.data; //获取选中的数据
    switch (obj.event) {
        case 'add':
            layer.msg('添加');
            isadd = true;
            openEdit(2);
            break;
        case 'update':
            if (data.length === 0) {
                layer.msg('请选择一行');
            } else if (data.length > 1) {
                layer.msg('只能同时编辑一个');
            } else {
                toEdit = checkStatus.data[0];
                isadd = false;
                openEdit(2);
            }
            break;
        case 'delete':
            if (data.length === 0) {
                layer.msg('请选择一行');
            } else {
                console.log(checkStatus.data);
                layer.confirm('真的删除行么', function (index) {
                    for (var deletedata of checkStatus.data) {
                        console.log(deletedata);
                        delData(deletedata.id);
                    }
                    layer.close(index);
                    showDetail(condition);
                    reFresh();
                });
            }
            break;
    }
});



















//获取文件id数组字符串
function getFidsStr(fids){
    var restr="";
    var i=0;
    for(var fid of fids){
        if(i>0)restr+=",";
        restr+="\""+fid.id+"\"";
        i++;
    }
    return restr;
}